//
//  ARPictureController.m
//  ARPictureView
//
//  Created by 包日守 on 15/11/5.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARPictureController.h"

#define BOUNDS [UIScreen mainScreen].bounds.size

@interface ARPictureController ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *pictureScroll;
@property (nonatomic,weak) UIPageControl *pageControl;
@end


@implementation ARPictureController





- (void) setIndex:(NSInteger)index
{
    _index = index;
    
    self.pictureScroll.contentOffset = CGPointMake(index * BOUNDS.width, 0);
    self.pageControl.currentPage = index;
    
    
 

}

- (UIPageControl *) pageControl
{
    if (_pageControl == nil) {
        
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.center = CGPointMake(BOUNDS.width/2, BOUNDS.height - 30);
        
        if (self.arrayOfImages.count <= 1 && self.arrayOfTitleOfImages.count <= 1) {
            
        }else
        {
        
            [self.view addSubview:pageControl];
        }
        _pageControl = pageControl;
        
        [_pageControl addTarget:self action:@selector(changePageControlValue) forControlEvents:UIControlEventValueChanged];
        
    }
    return _pageControl;

}

- (UIScrollView *) pictureScroll
{
    
    if (_pictureScroll == nil) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:scrollView];
        _pictureScroll = scrollView;
        _pictureScroll.delegate = self;
        _pictureScroll.showsHorizontalScrollIndicator = NO;
        _pictureScroll.pagingEnabled = YES;
        
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(exit:)];
        
        [self.pictureScroll addGestureRecognizer:tap];
    }
    return _pictureScroll;

}


- (void) exit:(UITapGestureRecognizer *) tap
{
    [self dismissViewControllerAnimated:NO completion:nil];

}

- (void) setArrayOfTitleOfImages:(NSArray *)arrayOfTitleOfImages
{
    _arrayOfTitleOfImages = arrayOfTitleOfImages;
    
    
    for (int i = 0; i < arrayOfTitleOfImages.count; i ++) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * BOUNDS.width, 0, BOUNDS.width, BOUNDS.height)];
        [self.pictureScroll addSubview:scrollView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        imageView.image = [UIImage imageNamed:arrayOfTitleOfImages[i]];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [scrollView addSubview:imageView];
        scrollView.contentSize = [UIScreen mainScreen].bounds.size;
        scrollView.delegate = self;
        
        scrollView.maximumZoomScale = 1.5;
        scrollView.minimumZoomScale = 1;
        
        
        
    }
    
    self.pictureScroll.contentSize = CGSizeMake(BOUNDS.width * arrayOfTitleOfImages.count, BOUNDS.height);
    
    
    self.pageControl.numberOfPages = arrayOfTitleOfImages.count;
    
    

}


- (void) setArrayOfImages:(NSArray *)arrayOfImages
{
    _arrayOfImages = arrayOfImages;
    
    
    for (int i = 0; i < arrayOfImages.count; i ++) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(i * BOUNDS.width, 0, BOUNDS.width, BOUNDS.height)];
        [self.pictureScroll addSubview:scrollView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        imageView.image = arrayOfImages[i];
        
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [scrollView addSubview:imageView];
        scrollView.contentSize = [UIScreen mainScreen].bounds.size;
        scrollView.delegate = self;
        
        scrollView.maximumZoomScale = 1.5;
        scrollView.minimumZoomScale = 1;
        
        
        
    }
    
    self.pictureScroll.contentSize = CGSizeMake(BOUNDS.width * arrayOfImages.count, BOUNDS.height);
    
    
    self.pageControl.numberOfPages = arrayOfImages.count;


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.view.backgroundColor = [UIColor blackColor];
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) changePageControlValue
{
    self.pictureScroll.contentOffset = CGPointMake(self.pageControl.currentPage * BOUNDS.width, 0);
    
    self.index = self.pageControl.currentPage;


}


#pragma mark - scrollViewDelegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    
    NSArray *array = nil;
    if (self.arrayOfImages.count > 0) {
        array = self.arrayOfImages;
    }
    if (self.arrayOfTitleOfImages.count > 0) {
        array = self.arrayOfTitleOfImages;
    }
    
    
    if (scrollView == _pictureScroll) {
        
        int index = scrollView.contentOffset.x / BOUNDS.width;
        
        if (index > 0) {
            UIScrollView *sc = scrollView.subviews[index - 1];
            sc.zoomScale = 1;
        }
        
        
        
        if (index < array.count -1) {
            UIScrollView *sc = scrollView.subviews[index + 1];
            sc.zoomScale = 1;
        }

        
        

        self.pageControl.currentPage = index;
        
        self.index = index;
        
        
        
        

        
        
    }

}

- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
        NSArray *array = scrollView.subviews;
        
        return array[0];
    
    

}


- (NSArray <id <UIPreviewActionItem>> *)previewActionItems {
    
    
    UIPreviewAction *action = [UIPreviewAction actionWithTitle:@"保存到相册" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //handler
        
        if (self.arrayOfImages.count > 0) {
            [self saveImageToPhotos:self.arrayOfImages[self.index]];
        }
        if (self.arrayOfTitleOfImages.count > 0) {
            [self saveImageToPhotos:[UIImage imageNamed:self.arrayOfTitleOfImages[self.index]]];
        }
        
    }];
    
    /*
    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"Share to Redpsace" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //handler
    }];
    
    UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"Share to iOS team" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        //handler
    }];
     */
    /*
    UIPreviewActionGroup *group1 = [UIPreviewActionGroup actionGroupWithTitle:@"Action Group" style:UIPreviewActionStyleDefault actions:@[action2, action3, action]];
     */
    
    return @[action];//, action3, group1];
}


- (void)saveImageToPhotos:(UIImage*)savedImage

{
    
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}

// 指定回调方法

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
    
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg = @"保存图片失败" ;
        
    }else{
        
        msg = @"保存图片成功" ;
        
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                          
                                                    message:msg
                          
                                                   delegate:self
                          
                                          cancelButtonTitle:@"确定"
                          
                                          otherButtonTitles:nil];
    
    [alert show];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
